// pages/carTrip/list/list.js
const { request } = require('../../../utils/request');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaName: '全部校区', // 所选校区
    timeName: '全部时间', // 所选时间
    lineList: [], // 路线列表
    lineCount: 0, // 当前路线数量
    selectTxtBlue:'selectTxtBlue', //全部校区的样式
    selectFuBlue:'selectFuBlue',
    placeSelect: false,
    dateSelect: false,
    areaItems: [
      {value: '南山校区(北门)', name: '南山校区(北门)', checked: false},
      {value: '南山校区(东门)', name: '南山校区(东门)', checked: false},
      {value: '新校区(西门)', name: '新校区(西门)', checked: false}
    ],
    timeItems: [
      {value: '上午(06:00-12:00)', name: '上午(06:00-12:00)', checked: false},
      {value: '中午(12:00-14:00)', name: '中午(12:00-14:00)', checked: false},
      {value: '下午(14:00-16:00)', name: '下午(14:00-16:00)', checked: false},
      {value: '晚间(19:00-21:00)', name: '晚间(19:00-21:00)', checked: false}
    ],
    lastSelectedAreaValues: [], // 保存上次选中的校区值
    lastSelectedTimeValues: [], // 保存上次选中的时间值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow() {
    this.getSchoolBusList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 在页面隐藏时，将所有复选框状态和保存的过滤条件重置
    const areaItems = this.data.areaItems.map(item => ({ ...item, checked: false }));
    const timeItems = this.data.timeItems.map(item => ({ ...item, checked: false }));
    this.setData({
      areaItems: areaItems,
      timeItems: timeItems,
      lastSelectedAreaValues: [], // 清空保存的校区过滤条件
      lastSelectedTimeValues: [], // 清空保存的时间过滤条件
      areaName: '全部校区', // 重置显示的校区名称
      timeName: '全部时间' // 重置显示的时间名称
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  //改变全部校区
  changeCompus(){
    if(this.data.dateSelect){
      this.setData({
        dateSelect:false
      })
    }
    // 打开弹窗时恢复保存的选中状态
    const areaItems = this.data.areaItems.map(item => ({
      ...item,
      checked: this.data.lastSelectedAreaValues.includes(item.value)
    }));
    this.setData({
      placeSelect: !this.data.placeSelect,
      areaItems: areaItems
    }) 
  },
  //改变全部时间
  changeTime(){
    if(this.data.placeSelect){
        this.setData({
          placeSelect:false
        })
    }
    // 打开弹窗时恢复保存的选中状态
    const timeItems = this.data.timeItems.map(item => ({
      ...item,
      checked: this.data.lastSelectedTimeValues.includes(item.value)
    }));
    this.setData({
      dateSelect:!this.data.dateSelect,
      timeItems: timeItems
    })
  },
  //接受checkbox的值
  checkPlaceValue(e){
      const values = e.detail.value;
      const areaItems = this.data.areaItems;
      areaItems.forEach(item => {
        item.checked = values.includes(item.value);
      });
      this.setData({
        areaItems: areaItems
      })
  },
  checkTimeValue(e){
    const values = e.detail.value;
    const timeItems = this.data.timeItems;
    timeItems.forEach(item => {
      item.checked = values.includes(item.value);
    });
    this.setData({
      timeItems: timeItems
    })
  },
  //清空
  clearPlace(){
    const areaItems = this.data.areaItems.map(item => ({
      ...item,
      checked: false
    }));
    this.setData({
      areaItems: areaItems,
      lastSelectedAreaValues: [],
      areaName: '全部校区'
    })
    this.getSchoolBusList()
  },
  clearTime(){
    const timeItems = this.data.timeItems.map(item => ({
      ...item,
      checked: false
    }));
    this.setData({
      timeItems: timeItems,
      lastSelectedTimeValues: [],
      timeName: '全部时间'
    })
    this.getSchoolBusList()
  },
  //确认
  confirmPlace(){
   const selectedValues = this.data.areaItems.filter(item => item.checked).map(item => item.value);
    this.setData({
      placeSelect:false,
      lastSelectedAreaValues: selectedValues // 保存选中的值
    })
    if(selectedValues.length > 0){
      if(selectedValues.length==1){
        this.setData({
          areaName:selectedValues[0]
        })
      }else{
        this.setData({
          areaName:'校区(多选)'
        })
      }
    } else {
      this.setData({
        areaName:'全部校区'
      })
    }
    this.getSchoolBusList()
  },
  confirmTime(){
    const selectedValues = this.data.timeItems.filter(item => item.checked).map(item => item.value);
    this.setData({
      dateSelect:false,
      lastSelectedTimeValues: selectedValues // 保存选中的值
    })
    if(selectedValues.length > 0){
      if(selectedValues.length==1){
        this.setData({
          timeName:selectedValues[0]
        })
      }else{
        this.setData({
          timeName:'时间(多选)'
        })
      }
    } else {
      this.setData({
        timeName:'全部时间'
      })
    }
    this.getSchoolBusList()
  },
  // 获取路线列表
  getSchoolBusList() { 
    let place = '';
    let time = '';
    const selectedPlaceValues = this.data.lastSelectedAreaValues;
    const selectedTimeValues = this.data.lastSelectedTimeValues;

    if(selectedPlaceValues.length > 0 || selectedTimeValues.length > 0){
      let p = selectedTimeValues.map(item=>{
        item=item.substring(0,2)
        return item;
      })
      place=selectedPlaceValues.join(',')
      time=p.join(',')
    }
    request({
      url: 'getSchoolBusList',
      method: 'POST',
      data: {
        place,
        time
      }
    }).then(data => {
      this.setData({
        lineList: data.list,
        lineCount: data.favoriteCount,
      })
    })
    .catch(e => {
      console.log('getSchoolBusList error::', e);
      wx.showToast({ title: e.message, icon: 'none' });
    });
  },

  // 收藏事件
  favoriteSchoolBus(busNo, title) {
    request({
      url: 'favoriteSchoolBus',
      method: 'POST',
      data: {
        busNo,
      }
    }).then(() => {
      wx.showToast({ title, icon: 'none' });
      this.getSchoolBusList();
    })
    .catch(e => {
      console.log('favoriteSchoolBus error::', e);
      wx.showToast({ title: e.message, icon: 'none' });
    });
  },

  // 添加收藏
  addCollect(busNo) {
    this.favoriteSchoolBus(busNo, '已收藏');
  },

  // 取消收藏
  cancelCollect(busNo) {
    wx.showModal({
      title: '是否确认从收藏夹中移除？',
      cancelColor: '#989898',
      confirmColor: '#2C70F8',
      success: (res) => {
        if (res.confirm) this.favoriteSchoolBus(busNo, '已取消');
      }
    })
  },

  // 收藏与取消
  collectHandle(e) {
    const { busno: busNo, favorite } = e.currentTarget.dataset;

    if (favorite === 0) this.addCollect(busNo);
    else this.cancelCollect(busNo);
  },

  // 跳转详情
  goDetail(e) {
    const url = e.currentTarget.dataset.url;
    // wx.
    wx.navigateTo({
      url,
    })
  }
})